package com.ruoyi.mymod.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserPerm {
    public Connection conn;

    public String getUserp(String phone) {
        String retStr=null;
        String url = "jdbc:oracle:thin:@10.104.8.7:1521:orcl"; // 使用服务名
        String user = "ctcnfd";
        String password = "ctcnfd";
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn= DriverManager.getConnection(url, user, password);
        } catch (Exception ex) {
            if (conn!=null)
            {
                try{
                    conn.close();
                } catch (Exception exx){

                }
            }
            return retStr;
        }

        Userp userp = new Userp();
        String sqlstr = "select t.username,t.arealevel,t.areacode,t.areaname,t.feedtypelevel,t.feedtypecode,t.feedtypename"+
                " from ai_user_feed_perm t where phone='" + phone + "'";

        Statement stmt = null;
        ResultSet rs = null;
        try{
            stmt = conn.createStatement();
            rs=stmt.executeQuery(sqlstr);
            if (rs.next()) {
                userp.phone = phone;
                userp.name = rs.getString(1);
                userp.areaLevel = rs.getInt(2);
                userp.areaCode = rs.getString(3);
                userp.areaName = rs.getString(4);
                userp.feedLevel = rs.getInt(5);
                userp.feedCode = rs.getString(6);
                userp.feedName = rs.getString(7);
                userp.areas=new ArrayList<Perm>();
                userp.feeds=new ArrayList<Perm>();
                getPerms(userp.areas,"AI_AREA",userp.areaLevel, userp.areaCode);
                getPerms(userp.feeds,"AI_FEEDTYPE",userp.feedLevel, userp.feedCode);

                retStr =  new ObjectMapper().writeValueAsString(userp);

            }
            else {
                return retStr=null;
            }
        } catch (Exception ex){

        }
        finally {
            if (stmt!=null)
                try{stmt.close();
                }catch (Exception ex1){
                    System.out.println(ex1.getMessage());
                }
            if (rs!=null)
                try{stmt.close();
                }catch (Exception ex2){
                    System.out.println(ex2.getMessage());
                }
            return retStr;
        }
      }

    public  void getPerms(List<Perm> perms,String tableName,int level, String code)  {

        String sqlstr="select code,name,lev from "+tableName+" t where t.parent='"+code+"'" ;
        Statement stmt = null;
        ResultSet rs = null;
        try{
            stmt=conn.createStatement();
            rs =stmt.executeQuery(sqlstr);
            while (rs.next()) {
                Perm perm = new Perm();
                perm.type=tableName;
                perm.code = rs.getString(1);
                perm.name = rs.getString(2);
                perm.level = rs.getInt(3);
                perm.children = new ArrayList<Perm>();
                perms.add(perm);
                getPerms(perms, tableName,perm.level, perm.code);

            }
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        finally {
            if (stmt!=null)
                try{stmt.close();
                }catch (Exception ex1){
                    System.out.println(ex1.getMessage());
                }
            if (rs!=null)
                try{rs.close();
                }catch (Exception ex2){
                    System.out.println(ex2.getMessage());
                }
        }

    }
}

class Userp{
    public String phone;
    public String name;
    public int areaLevel;
    public String areaCode;
    public String areaName;
    public int feedLevel;
    public String feedCode;
    public String feedName;
    public List<Perm> areas;
    public List<Perm> feeds;
}


class Perm{
    public String type;
    public int level;
    public String code;
    public String name;
    public List<Perm> children ;
}
